﻿using System;
using System.Collections.Generic;

namespace Terraria.ID
{
	// Token: 0x02000376 RID: 886
	public class SetFactory
	{
		// Token: 0x06001FBD RID: 8125 RVA: 0x004584EC File Offset: 0x004566EC
		public SetFactory(int size)
		{
			this._size = size;
		}

		// Token: 0x06001FBE RID: 8126 RVA: 0x00458540 File Offset: 0x00456740
		protected bool[] GetBoolBuffer()
		{
			object queueLock = this._queueLock;
			bool[] result;
			lock (queueLock)
			{
				if (this._boolBufferCache.Count == 0)
				{
					result = new bool[this._size];
				}
				else
				{
					result = this._boolBufferCache.Dequeue();
				}
			}
			return result;
		}

		// Token: 0x06001FBF RID: 8127 RVA: 0x004585A4 File Offset: 0x004567A4
		protected int[] GetIntBuffer()
		{
			object queueLock = this._queueLock;
			int[] result;
			lock (queueLock)
			{
				if (this._intBufferCache.Count == 0)
				{
					result = new int[this._size];
				}
				else
				{
					result = this._intBufferCache.Dequeue();
				}
			}
			return result;
		}

		// Token: 0x06001FC0 RID: 8128 RVA: 0x00458608 File Offset: 0x00456808
		protected ushort[] GetUshortBuffer()
		{
			object queueLock = this._queueLock;
			ushort[] result;
			lock (queueLock)
			{
				if (this._ushortBufferCache.Count == 0)
				{
					result = new ushort[this._size];
				}
				else
				{
					result = this._ushortBufferCache.Dequeue();
				}
			}
			return result;
		}

		// Token: 0x06001FC1 RID: 8129 RVA: 0x0045866C File Offset: 0x0045686C
		protected float[] GetFloatBuffer()
		{
			object queueLock = this._queueLock;
			float[] result;
			lock (queueLock)
			{
				if (this._floatBufferCache.Count == 0)
				{
					result = new float[this._size];
				}
				else
				{
					result = this._floatBufferCache.Dequeue();
				}
			}
			return result;
		}

		// Token: 0x06001FC2 RID: 8130 RVA: 0x004586D0 File Offset: 0x004568D0
		public void Recycle(bool[] buffer)
		{
			object queueLock = this._queueLock;
			lock (queueLock)
			{
				this._boolBufferCache.Enqueue(buffer);
			}
		}

		// Token: 0x06001FC3 RID: 8131 RVA: 0x00458718 File Offset: 0x00456918
		public void Recycle(int[] buffer)
		{
			object queueLock = this._queueLock;
			lock (queueLock)
			{
				this._intBufferCache.Enqueue(buffer);
			}
		}

		// Token: 0x06001FC4 RID: 8132 RVA: 0x0001642E File Offset: 0x0001462E
		public bool[] CreateBoolSet(params int[] types)
		{
			return this.CreateBoolSet(false, types);
		}

		// Token: 0x06001FC5 RID: 8133 RVA: 0x00458760 File Offset: 0x00456960
		public bool[] CreateBoolSet(bool defaultState, params int[] types)
		{
			bool[] boolBuffer = this.GetBoolBuffer();
			for (int i = 0; i < boolBuffer.Length; i++)
			{
				boolBuffer[i] = defaultState;
			}
			for (int j = 0; j < types.Length; j++)
			{
				boolBuffer[types[j]] = !defaultState;
			}
			return boolBuffer;
		}

		// Token: 0x06001FC6 RID: 8134 RVA: 0x004587A0 File Offset: 0x004569A0
		public int[] CreateIntSet(int defaultState, params int[] inputs)
		{
			if (inputs.Length % 2 != 0)
			{
				throw new Exception("You have a bad length for inputs on CreateArraySet");
			}
			int[] intBuffer = this.GetIntBuffer();
			for (int i = 0; i < intBuffer.Length; i++)
			{
				intBuffer[i] = defaultState;
			}
			for (int j = 0; j < inputs.Length; j += 2)
			{
				intBuffer[inputs[j]] = inputs[j + 1];
			}
			return intBuffer;
		}

		// Token: 0x06001FC7 RID: 8135 RVA: 0x004587F4 File Offset: 0x004569F4
		public ushort[] CreateUshortSet(ushort defaultState, params ushort[] inputs)
		{
			if (inputs.Length % 2 != 0)
			{
				throw new Exception("You have a bad length for inputs on CreateArraySet");
			}
			ushort[] ushortBuffer = this.GetUshortBuffer();
			for (int i = 0; i < ushortBuffer.Length; i++)
			{
				ushortBuffer[i] = defaultState;
			}
			for (int j = 0; j < inputs.Length; j += 2)
			{
				ushortBuffer[(int)inputs[j]] = inputs[j + 1];
			}
			return ushortBuffer;
		}

		// Token: 0x06001FC8 RID: 8136 RVA: 0x00458848 File Offset: 0x00456A48
		public float[] CreateFloatSet(float defaultState, params float[] inputs)
		{
			if (inputs.Length % 2 != 0)
			{
				throw new Exception("You have a bad length for inputs on CreateArraySet");
			}
			float[] floatBuffer = this.GetFloatBuffer();
			for (int i = 0; i < floatBuffer.Length; i++)
			{
				floatBuffer[i] = defaultState;
			}
			for (int j = 0; j < inputs.Length; j += 2)
			{
				floatBuffer[(int)inputs[j]] = inputs[j + 1];
			}
			return floatBuffer;
		}

		// Token: 0x06001FC9 RID: 8137 RVA: 0x0045889C File Offset: 0x00456A9C
		public T[] CreateCustomSet<T>(T defaultState, params object[] inputs)
		{
			if (inputs.Length % 2 != 0)
			{
				throw new Exception("You have a bad length for inputs on CreateCustomSet");
			}
			T[] array = new T[this._size];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = defaultState;
			}
			if (inputs != null)
			{
				for (int j = 0; j < inputs.Length; j += 2)
				{
					array[(int)inputs[j]] = (T)((object)inputs[j + 1]);
				}
			}
			return array;
		}

		// Token: 0x04003810 RID: 14352
		protected int _size;

		// Token: 0x04003811 RID: 14353
		private Queue<int[]> _intBufferCache = new Queue<int[]>();

		// Token: 0x04003812 RID: 14354
		private Queue<ushort[]> _ushortBufferCache = new Queue<ushort[]>();

		// Token: 0x04003813 RID: 14355
		private Queue<bool[]> _boolBufferCache = new Queue<bool[]>();

		// Token: 0x04003814 RID: 14356
		private Queue<float[]> _floatBufferCache = new Queue<float[]>();

		// Token: 0x04003815 RID: 14357
		private object _queueLock = new object();
	}
}
